package ${package.Controller};

import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.vo.Result;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;

import javax.annotation.Resource;

/**
 * <p>
 * ${cfg.title} 控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@Api(value="/${cfg.controllerUrl}/${cfg.entityMin}", tags = "${cfg.title}")
@RequestMapping(value="/${cfg.controllerUrl}/${cfg.entityMin}")
public class ${table.controllerName} extends BaseController<${entity}>{

    @Resource
    private ${table.serviceName} ${cfg.entityMin}Service;


    /**
     * 查询
     * @return
     */
    @GetMapping(value = "/select")
    @ApiOperation(value = "查询接口", notes = "查询接口" , httpMethod = "GET")
    public Result<?> select(${entity} entity){
        entity.setOid(entity.getOid() == null ? getUser().getLastOid() : entity.getOid());
        LambdaQueryWrapper<${entity}> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(${entity}::getCreateDate);
        IPage<${entity}> iPage = ${cfg.entityMin}Service.page(getPage(), wrapper);
        return Result.page(iPage.getRecords(), iPage.getTotal());
    }

    /**
     * 查询单个
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation(value = "查询单个接口", notes = "查询单个接口" , httpMethod = "GET")
    public Result<?> selectById(${entity} entity){
        ${entity} by = ${cfg.entityMin}Service.getById(entity.getId());
        return Result.success(by);
    }

    /**
     * 新增
     * @return
     */
    @LogData(module = "${cfg.title}", remarks = "新增")
    @PostMapping("/add")
    @ApiOperation(value = "新增接口", notes = "新增接口" , httpMethod = "POST")
    public Result<?> add(${entity} entity){
        entity.setOid(entity.getOid() == null ? getUser().getLastOid() : entity.getOid());
        ${cfg.entityMin}Service.save(entity);
        return Result.success(Result.ADD_MSG);
    }

    /**
     * 修改
     * @return
     */
    @LogData(module = "${cfg.title}", remarks = "修改")
    @PostMapping("/edit")
    @ApiOperation(value = "修改接口", notes = "修改接口" , httpMethod = "POST")
    public Result<?> edit(${entity} entity){
        ${entity} byId = ${cfg.entityMin}Service.getById(entity.getId());
        entity.setVersion(byId.getVersion());// 使用乐观锁
        ${cfg.entityMin}Service.updateById(entity);
        return Result.success(Result.UPDATE_MSG);
    }

    /**
     * 删除
     * @return
     */
    @LogData(module = "${cfg.title}", remarks = "删除单个")
    @DeleteMapping("/delete")
    @ApiOperation(value = "单个删除接口", notes = "单个删除接口" , httpMethod = "POST")
    public Result<?> delete(String id){
        ${cfg.entityMin}Service.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @return
     */
    @LogData(module = "${cfg.title}", remarks = "批量删除")
    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除接口", notes = "批量删除接口" , httpMethod = "POST")
    public Result<?> deleteBatch(String ids){
        ${cfg.entityMin}Service.removeByIds(Arrays.asList(ids.split(",")));
        return Result.success();
    }

    

}
